package com.guofen.service.yf.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.RdDischargeMapper;
import com.guofen.pojo.RdDischarge;
import com.guofen.service.yf.RdDischargeService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RdDischargeimpl implements RdDischargeService {
    @Autowired
    private RdDischargeMapper rdDischargeMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAllInfo(RdDischarge rdDischarge) {
        int flag = rdDischargeMapper.insert(rdDischarge);
        if (flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public RdDischarge getlast() {
        return rdDischargeMapper.getlast();
    }

    @Override
    public boolean addFirst(String dischargeDate, String batch, String zubie, String boxNumber, String device1Name, String dischargeStarttime, String dischargeEndtime, String sieveStarttime, String sieveEndtime, String dischargeBoxWeight, String boxWeight, String dischargeWeight, String insectDungWeight_1, String sieveWeight, String surfaceFiberWeight, String insectImpurityWeight, String remark, String recorder) {
        if (rdDischargeMapper.getlast() != null){
            RdDischarge rdDischarge2 = new RdDischarge();
            rdDischarge2.setId(rdDischargeMapper.getlast().getId());
            rdDischarge2.setDischargeEndtime(dischargeEndtime);
            rdDischargeMapper.updateByPrimaryKeySelective(rdDischarge2);
        }
        RdDischarge rdDischarge = new RdDischarge();
        rdDischarge.setDischargeDate(dischargeDate);
        rdDischarge.setBatch(batch);
        rdDischarge.setZubie(Integer.parseInt(zubie));
        rdDischarge.setBoxNumber(Integer.parseInt(boxNumber));
        rdDischarge.setDeviceName1(device1Name);
        if (rdDischargeMapper.getlast() != null){
            rdDischarge.setDischargeStarttime(rdDischargeMapper.getlast().getDischargeStarttime());
        }else {
            rdDischarge.setDischargeStarttime(dischargeStarttime);
        }
        rdDischarge.setDischargeEndtime(dischargeEndtime);
        rdDischarge.setSieveStarttime(sieveStarttime);
        rdDischarge.setSieveEndtime(sieveEndtime);
        rdDischarge.setDischargeBoxWeight(Double.parseDouble(dischargeBoxWeight));
        rdDischarge.setBoxWeight(Double.parseDouble(boxWeight));
        rdDischarge.setDischargeWeight(Double.parseDouble(dischargeWeight));
        rdDischarge.setInsectDungWeight1(Double.parseDouble(insectDungWeight_1));
        rdDischarge.setSieveWeight(Double.parseDouble(sieveWeight));
        rdDischarge.setSurfaceFiberWeight(Double.parseDouble(surfaceFiberWeight));
        rdDischarge.setInsectImpurityWeight(Double.parseDouble(insectImpurityWeight));
        rdDischarge.setRecorder(recorder);
        rdDischarge.setRemark(remark);
        /*for (RdDischarge rdDischarge1 : rdDischargeMapper.getListToday()){
            RdDischarge rdDischarge2 = new RdDischarge();
            rdDischarge2.setId(rdDischarge1.getId());
            rdDischarge2.setSieveEndtime(sieveStarttime);
            rdDischargeMapper.updateByPrimaryKeySelective(rdDischarge2);
        }*/
        int flag = rdDischargeMapper.insertSelective(rdDischarge);
        if (flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<RdDischarge> getList() {
        return rdDischargeMapper.getList();
    }

    @Override
    public boolean addSecond(String id, String seperateDate, String insectSeparateStarttime, String insectSeparateEndtime, String device2Name, String insectWeight, String impurityWeight, String insectDungWeight, String remark, String separatePerson) {
        RdDischarge rdDischarge1 = rdDischargeMapper.selectByPrimaryKey(Integer.parseInt(id));
        String str = rdDischarge1.getRemark();
        RdDischarge rdDischarge = new RdDischarge();
        rdDischarge.setId(Integer.parseInt(id));
        rdDischarge.setSeperateDate(seperateDate);
        rdDischarge.setInsectSeparateStarttime(insectSeparateStarttime);
        rdDischarge.setInsectSeparateEndtime(insectSeparateEndtime);

        rdDischarge.setInsectWeight(Double.parseDouble(insectWeight));
        rdDischarge.setImpurityWeight(Double.parseDouble(impurityWeight));

        rdDischarge.setRemark(str+"|"+remark);
        rdDischarge.setSeparatePerson(separatePerson);
        int flag = rdDischargeMapper.updateByPrimaryKeySelective(rdDischarge);
        if (flag > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<RdDischarge> list = rdDischargeMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<RdDischarge> list = rdDischargeMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return rdDischargeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(RdDischarge record) {
        return rdDischargeMapper.updateByPrimaryKeySelective(record);
    }
}
